﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using WPFExcelReport;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXBL : BaseFunction
    {
        DataSet SResult;
        DataSet SReturn;
        DataTable TbScriber = null;
        DataTable TbEventdef = null; 
       public string UserID
        {
            get
            {
                return app.User;
            }
        }
        private DataRow NewICC_WFEVENTSRow()
        {
            DataRow dwData = null;
            dwData = INTEXDB.ICC_WFEVENTS.NewICC_WFEVENTSRow();
            dwData["MANDANT"] = "".PadRight(2);
            dwData["ROWNID"] = "".PadRight(20);
            dwData["CONTROL"] = "NO ";
            dwData["USERGROUP"] = "".PadRight(3);
            dwData["WFEUSER"] = "".PadRight(8);
            dwData["WFEVENTS"] = "".PadRight(20);
            dwData["WFEHANDLER"] = "".PadRight(20);
            dwData["WFSOURCE"] = "".PadRight(200);
            dwData["WFWHEN"] = "".PadRight(20);
            dwData["WHAT"] = "".PadRight(800);
            dwData["HOW"] = "".PadRight(20);
            dwData["ORDTYPE"] = "".PadRight(2);
            dwData["KRS"] = "".PadRight(2);
            dwData["LFD"] = "".PadRight(6);
            dwData["POS"] = "".PadRight(3);
            dwData["ARTTYPE"] = "".PadRight(1);
            dwData["ARTICLE"] = "".PadRight(20);
            dwData["ARTDES"] = "".PadRight(40);
            dwData["VAR"] = "".PadRight(3);
            dwData["COLOR"] = "".PadRight(10);
            dwData["CDES"] = "".PadRight(40);
            dwData["ASIZE"] = "".PadRight(7);
            dwData["FINISH"] = "".PadRight(10);
            dwData["FINDES"] = "".PadRight(40);
            dwData["P1"] = "".PadRight(40);
            dwData["P2"] = "".PadRight(40);
            dwData["P3"] = "".PadRight(40);
            dwData["P4"] = "".PadRight(40);
            dwData["P5"] = "".PadRight(40);
            dwData["P6"] = "".PadRight(40);
            dwData["NEWUSER"] = "".PadRight(8);
            dwData["NEWTIME"] = "".PadRight(20);
            dwData["CHGUSER"] = "".PadRight(8);
            dwData["CHGTIME"] = "".PadRight(20);
            return dwData;
        }
        public void CheckEvents(string events, DataRow dwInput, string msg = "", MessageInfo msgs = null)
        {
            if (!app.Connected)
                return;

            if (TbScriber == null)
            {
                SResult = INTEXDBC.QueryICC_WFSUBSCRIBER(null, 9999);
                if (SResult.Tables.Count > 0)
                {
                    TbScriber = SResult.Tables[0].Copy();
                }
                else
                    return;
            }
            if (TbEventdef == null)
            {
                SResult = INTEXDBC.QueryICC_WFEVENTDEF(null, 9999);
                if (SResult.Tables.Count > 0)
                {
                    TbEventdef = SResult.Tables[0].Copy();
                }
                else
                    return;
            }
            switch (events)
            {
                case "A003001":
                case "A003010":
                case "A003020":
                case "A003030":
                case "A003040":
                case "A003050":
                case "A003060":
                    EA003001(dwInput, events, msg);
                    break; 
                case "A001010":
                    EA001010(dwInput, events, msg);
                    break;
                case "A001011":
                    EA001011(dwInput, events, msg);
                    break;
                case "A001013":
                    EA001013(dwInput, events, msg);
                    break;
                case "A001014":
                    EA001014(dwInput, events, msg);
                    break;
                case "A001020":
                    EA001020(dwInput, events, msg);
                    break;
                case "A001021":
                    EA001021(dwInput, events, msg);
                    break;
                case "A001022":
                    EA001022(dwInput, events, msg);
                    break;
                case "A002010":
                    EA002010(dwInput, events, msg, msgs);
                    break;
                case "M002001":
                    EM002001(dwInput, events, msg, msgs);
                    break;

                case "M002003":
                    EM002003(dwInput, events, msg, msgs);
                    break;
                case "M002002":
                case "M002004":
                case "M002005":
                case "M002006":
                    EM002002(dwInput, events, msg, msgs);
                    break;
                case "A999001":
                    EA999001(dwInput, events, msg, msgs);
                    break;
                case "A901001":
                case "A901002":
                case "A901003":
                case "A901004":
                    EA901001(dwInput, events, msg, msgs);
                    break;
                case "A901301":
                    EA901301(dwInput, events, msg, msgs);
                    break;
                case "MYGRVKPOS001"://品质审核               
                case "MYGRVKPOS002"://颜色审核
                case "MYGRVKPOS003"://计划备料审核
                case "MYGRVKPOS004"://运输
                case "MYGRVKPOS005"://计划
                case "MYGRVKPOS006"://生产
                case "MYGRVKPOS007"://销售
                    EMYGRVKPOS001(dwInput, events, msg, msgs);
                    break;
                case "A901101":
                    EA901101(dwInput, events, msg, msgs);
                    break;
                case "A901102":
                    EA901102(dwInput, events, msg, msgs);
                    break;
            }
        }
        private void EA003001(DataRow dwInput, string events = "A001010", string msg = "")
        {
            try
            {
                if (!dwInput.Table.TableName.Contains("ABPOS"))
                    return;
                DataRow dwData = null;
                msg = ConvertGB2312To8859P1(msg);
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["ORDTYPE"] = "V";
                dwData["KRS"] = dwInput["ABPAUFKRS"];
                dwData["LFD"] = dwInput["ABPAUFLFD"];
                dwData["ARTTYPE"] = dwInput["ABPWAKZ"];
                dwData["ARTICLE"] = dwInput["ABPART"];
                dwData["COLOR"] = dwInput["ABPFARBE"];
                switch (dwInput["ABPWAKZ"].ToString())
                {
                    case "T":
                        //dwData["ARTDES"] = dwInput["ARTDES"];
                        //dwData["CDES"] = dwInput["CDES"];
                        break;
                    case "F":
                        break;
                }

                dwData["P1"] = Left("99".PadRight(40), 40);
                SResult = INTEXDBC.QueryICC_WFEVENTS(dwData, 99);
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    return;
                }
                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WHAT"] = Left(msg.PadRight(800), 800);                    
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void EA001010(DataRow dwInput, string events = "A001010", string msg = "")
        {
            try
            {
                if (dwInput.Table.TableName != "ABPOS")
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["ORDTYPE"] = "V";
                dwData["KRS"] = dwInput["ABPAUFKRS"];
                dwData["LFD"] = dwInput["ABPAUFLFD"];
                dwData["ARTTYPE"] = dwInput["ABPWAKZ"];
                dwData["ARTICLE"] = dwInput["ABPART"];
                dwData["COLOR"] = dwInput["ABPFARBE"];
                switch (dwInput["ABPWAKZ"].ToString())
                {
                    case "T":
                        //dwData["ARTDES"] = dwInput["ARTDES"];
                        //dwData["CDES"] = dwInput["CDES"];
                        break;
                    case "F":
                        break;
                }

                dwData["P1"] = Left("99".PadRight(40), 40);
                SResult = INTEXDBC.QueryICC_WFEVENTS(dwData, 99);
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    return;
                }
                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void EA001011(DataRow dwInput, string events = "A001011", string msg = "")
        {
            try
            {
                if (dwInput.Table.TableName != "ABPOS")
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["ORDTYPE"] = "V";
                dwData["KRS"] = dwInput["ABPAUFKRS"];
                dwData["LFD"] = dwInput["ABPAUFLFD"];
                dwData["ARTTYPE"] = dwInput["ABPWAKZ"];
                dwData["ARTICLE"] = dwInput["ABPART"];
                dwData["COLOR"] = dwInput["ABPFARBE"];
                switch (dwInput["ABPWAKZ"].ToString())
                {
                    case "T":
                        //dwData["ARTDES"] = dwInput["ARTDES"];
                        //dwData["CDES"] = dwInput["CDES"];
                        break;
                    case "F":
                        break;
                }

                dwData["P1"] = Left("99".PadRight(40), 40);
                SResult = INTEXDBC.QueryICC_WFEVENTS(dwData, 99);
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    return;
                }
                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void EA001013(DataRow dwInput, string events = "A001013", string msg = "")
        {
            try
            {
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["ARTICLE"] = dwInput["ITXFART"];
                dwData["COLOR"] = dwInput["ITXFCOLOR"];
                SResult = INTEXDBC.QueryICC_WFEVENTS(dwData, 3);
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    return;
                }
                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["WHAT"] = Left(msg.PadRight(800), 800);
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void EA001014(DataRow dwInput, string events = "A001014", string msg = "")
        {
            try
            {
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["ARTICLE"] = dwInput["WAFART"];
                dwData["COLOR"] = dwInput["WAFZUSATZ"];

                SResult = INTEXDBC.QueryICC_WFEVENTS(dwData, 3);
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    return;
                }
                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["NEWUSER"] = UserID;
                    dwData["WHAT"] = Left(msg.PadRight(800), 800);
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void EA001020(DataRow dwInput, string events = "A001020", string msg = "")
        {
            try
            {
                if (dwInput.Table.TableName != "ABPOS")
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["ORDTYPE"] = "V";
                dwData["KRS"] = dwInput["ABPAUFKRS"];
                dwData["LFD"] = dwInput["ABPAUFLFD"];
                dwData["ARTTYPE"] = dwInput["ABPWAKZ"];
                dwData["ARTICLE"] = dwInput["ABPART"];
                dwData["COLOR"] = dwInput["ABPFARBE"];
                switch (dwInput["ABPWAKZ"].ToString())
                {
                    case "T":
                        //dwData["ARTDES"] = dwInput["ARTDES"];
                        //dwData["CDES"] = dwInput["CDES"];
                        break;
                    case "F":
                        break;
                }

                dwData["P1"] = Left("89".PadRight(40), 40);
                SResult = INTEXDBC.QueryICC_WFEVENTS(dwData, 99);
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    return;
                }
                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }

                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void EA001021(DataRow dwInput, string events = "A001021", string msg = "")
        {
            try
            {
                if (dwInput.Table.TableName != "ABPOS")
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["ORDTYPE"] = "V";
                dwData["KRS"] = dwInput["ABPAUFKRS"];
                dwData["LFD"] = dwInput["ABPAUFLFD"];
                dwData["ARTTYPE"] = dwInput["ABPWAKZ"];
                dwData["ARTICLE"] = dwInput["ABPART"];
                dwData["COLOR"] = dwInput["ABPFARBE"];
                switch (dwInput["ABPWAKZ"].ToString())
                {
                    case "T":
                        //dwData["ARTDES"] = dwInput["ARTDES"];
                        //dwData["CDES"] = dwInput["CDES"];
                        break;
                    case "F":
                        break;
                }
                dwData["P1"] = Left("89".PadRight(40), 40);
                SResult = INTEXDBC.QueryICC_WFEVENTS(dwData, 99);
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    return;
                }
                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void EA001022(DataRow dwInput, string events = "A001022", string msg = "")
        {
            try
            {
                if (dwInput.Table.TableName != "ABPOS")
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["ORDTYPE"] = "V";
                dwData["KRS"] = dwInput["ABPAUFKRS"];
                dwData["LFD"] = dwInput["ABPAUFLFD"];
                dwData["ARTTYPE"] = dwInput["ABPWAKZ"];
                dwData["ARTICLE"] = dwInput["ABPART"];
                dwData["COLOR"] = dwInput["ABPFARBE"];
                switch (dwInput["ABPWAKZ"].ToString())
                {
                    case "T":
                        //dwData["ARTDES"] = dwInput["ARTDES"];
                        //dwData["CDES"] = dwInput["CDES"];
                        break;
                    case "F":
                        break;
                }
                dwData["P1"] = Left("89".PadRight(40), 40);
                SResult = INTEXDBC.QueryICC_WFEVENTS(dwData, 99);
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    return;
                }
                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void EA999001(DataRow dwInput, string events = "A001021", string msg = "", MessageInfo msgs = null)
        {
            try
            {
                if (msgs == null)
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["WFWHEN"] = System.DateTime.Now;
                string where = msgs.Where;
                int idx1 = 0, idx2 = 0;
                idx1 = where.IndexOf(@"\");
                if (idx1 > 0)
                {
                    where = msgs.Where.Substring(idx1 - 2);
                }
                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["WFSOURCE"] = Left(where.PadRight(200), 200);
                    string src =ConvertGB2312To8859P1(msgs.Details);
                    dwData["WHAT"] = Left(src.PadRight(800),800);
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void EMYGRVKPOS001(DataRow dwInput, string events = "MYGRVKPOS001", string msg = "", MessageInfo msgs = null)
        {
            try
            {
                if (msgs == null)
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["WFWHEN"] = System.DateTime.Now;
                dwData["ORDTYPE"] = "K";
                dwData["KRS"] = dwInput["VKPAUFKRS"];
                dwData["LFD"] = dwInput["VKPAUFLFD"];
                dwData["POS"] = dwInput["VKPAUFPOS"];
                string userid = msgs.Where;

                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["WHAT"] = Left(msg.PadRight(800), 800);
                    dwData["NEWUSER"] = userid;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    DataRow dwWF = INTEXDBC.SaveICC_WFEVENTS(dwData);
                    //FireEvent(dwWF);
                }
            }
            catch (Exception ex)
            {

            }
        }
        private void EA901001(DataRow dwInput, string events = "A001021", string msg = "", MessageInfo msgs = null)
        {
            try
            {
                if (msgs == null)
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["WFWHEN"] = System.DateTime.Now;
                dwData["ORDTYPE"] = "K";
                dwData["KRS"] = dwInput["VKPAUFKRS"];
                dwData["LFD"] = dwInput["VKPAUFLFD"];
                dwData["POS"] = dwInput["VKPAUFPOS"];
                string userid = msgs.Where;

                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["WHAT"] = Left(msgs.Details.PadRight(800), 800);
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    DataRow dwWF = INTEXDBC.SaveICC_WFEVENTS(dwData);
                    FireEvent(dwWF);
                }
            }
            catch (Exception ex)
            {

            }
        }
        private void FireEvent(DataRow dwMaster)
        {
            //不经过WorkFlowDocument,直接触发界面
            string WFEHANDLER = dwMaster["WFEHANDLER"].ToString();
            string WFEUSER = dwMaster["WFEUSER"].ToString();
            string WFEVENTS = dwMaster["WFEVENTS"].ToString();
            string ORDTYPE = dwMaster["ORDTYPE"].ToString();
            string KRS = dwMaster["KRS"].ToString();
            string LFD = dwMaster["LFD"].ToString();
            MessageInfo msg = new MessageInfo(MessageEventType.Info, WFEHANDLER, "WORKFLOW", dwMaster["ROWNID"].ToString());
            EventDispatcher.Instance.RaiseMessage(msg);
        }
        private void EM002001(DataRow dwInput, string events = "A001021", string msg = "", MessageInfo msgs = null)
        {
            try
            {
                if (msgs == null)
                    return;
                if (dwInput.Table.TableName != "EKKOPF")
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["WFWHEN"] = System.DateTime.Now;
                dwData["KRS"] = dwInput["EKKAUFKRS"];
                dwData["LFD"] = dwInput["EKKAUFLFD"];
                string where = msgs.Where;
                string what = msgs.What;

                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["WFSOURCE"] = Left(where.PadRight(200), 200);
                    dwData["WHAT"] = Left(what.PadRight(800), 800);
                    dwData["HOW"] = Left(msg.PadRight(20), 20);
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void EM002002(DataRow dwInput, string events = "A001021", string msg = "", MessageInfo msgs = null)
        {
            try
            {
                if (msgs == null)
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["WFWHEN"] = System.DateTime.Now;
                dwData["KRS"] = dwInput["EKKAUFKRS"];
                dwData["LFD"] = dwInput["EKKAUFLFD"];
                string where = msgs.Where;
                string what = msgs.What;

                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["WFSOURCE"] = Left(where.PadRight(200), 200);
                    dwData["WHAT"] = Left(msg.PadRight(800), 800);
                    dwData["HOW"] = Left(msg.PadRight(20), 20);
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void EA901301(DataRow dwInput, string events = "A901301", string msg = "", MessageInfo msgs = null)
        {
            try
            {
                if (msgs == null)
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["WFWHEN"] = System.DateTime.Now; 
                string where = msgs.Where;

                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["WHAT"] = dwInput["ABKKDAUFTNR"].ToString();
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    DataRow dwWF = INTEXDBC.SaveICC_WFEVENTS(dwData);
                    FireEvent(dwWF);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
            private void EA901101(DataRow dwInput, string events = "A901101", string msg = "", MessageInfo msgs = null)
        {
            try
            {
                if (msgs == null)
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["WFWHEN"] = System.DateTime.Now;
                dwData["KRS"] = dwInput["EKPAUFKRS"];
                dwData["LFD"] = dwInput["EKPAUFLFD"];
                dwData["POS"] = dwInput["EKPAUFPOS"];
                string where = msgs.Where;

                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["WHAT"] = Left(msg.PadRight(800), 800);
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    DataRow dwWF = INTEXDBC.SaveICC_WFEVENTS(dwData);
                    FireEvent(dwWF);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void EA901102(DataRow dwInput, string events = "A901102", string msg = "", MessageInfo msgs = null)
        {
            try
            {
                if (msgs == null)
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["WFWHEN"] = System.DateTime.Now;
                dwData["KRS"] = dwInput["EKPAUFKRS"];
                dwData["LFD"] = dwInput["EKPAUFLFD"];
                dwData["POS"] = dwInput["EKPAUFPOS"];
                string where = msgs.Where;
                string liefnr = msgs.Details;
                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["WHAT"] = Left(msg.PadRight(800), 800);
                    dwData["HOW"] = Left(liefnr.PadRight(20), 20);
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    DataRow dwWF = INTEXDBC.SaveICC_WFEVENTS(dwData);
                    FireEvent(dwWF);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void EM002003(DataRow dwInput, string events = "M002003", string msg = "", MessageInfo msgs = null)
        {
            try
            {
                if (msgs == null)
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["WFWHEN"] = System.DateTime.Now;
                dwData["KRS"] = dwInput["EKPAUFKRS"];
                dwData["LFD"] = dwInput["EKPAUFLFD"];
                dwData["POS"] = dwInput["EKPAUFPOS"];
                string where = msgs.Where;
                string how = msgs.What;

                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["WFSOURCE"] = Left(where.PadRight(200), 200);
                    dwData["WHAT"] = Left(msg.PadRight(800), 800);
                    dwData["HOW"] = Left(how.PadRight(20), 20);
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void EA002010(DataRow dwInput, string events = "A002010", string msg = "", MessageInfo msgs = null)
        {
            try
            {
                if (msgs == null)
                    return;
                if (dwInput.Table.TableName != "EKKOPF")
                    return;
                DataRow dwData = null;
                dwData = NewICC_WFEVENTSRow();
                dwData["ROWNID"] = 0;
                dwData["MANDANT"] = app.Mandant;
                dwData["WFEVENTS"] = Left(events.PadRight(20), 20);
                dwData["WFWHEN"] = System.DateTime.Now;
                dwData["KRS"] = dwInput["EKKAUFKRS"];
                dwData["LFD"] = dwInput["EKKAUFLFD"];
                string applier = dwInput["EKKNEUB"].ToString();
                DataRow dwEvents = null;
                var qHander = from DataRow row in this.TbEventdef.AsEnumerable()
                              where row["WFEVENTS"].ToString().Trim() == events
                              select row;
                foreach (DataRow row in qHander)
                {
                    dwEvents = row;
                }
                var qUsers = from DataRow row in TbScriber.AsEnumerable()
                             where row["WFEVENTS"].ToString().Trim() == events
                             && row["WFUSER"].ToString().Trim() == applier.Trim()
                             select row;
                foreach (DataRow row in qUsers)
                {
                    dwData["USERGROUP"] = row["USERGROUP"];
                    dwData["WFEUSER"] = row["WFEUSER"];
                    dwData["WFEHANDLER"] = dwEvents["WFEHANDLER"];
                    dwData["WHAT"] = dwEvents["WFEDES"];
                    dwData["NEWUSER"] = UserID;
                    dwData["NEWTIME"] = DateTime.Now.ToLocalTime().ToString();
                    INTEXDBC.SaveICC_WFEVENTS(dwData);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
